import { create } from 'zustand';

export interface TabItem {
  key: string;
  label: string;
  path: string;
  closable?: boolean;
  icon?: React.ReactNode;
}

interface TabsState {
  // 状态
  activeKey: string;
  tabs: TabItem[];

  // 操作
  addTab: (tab: TabItem) => void;
  removeTab: (key: string) => void;
  setActiveTab: (key: string) => void;
  closeOtherTabs: (key: string) => void;
  closeLeftTabs: (key: string) => void;
  closeRightTabs: (key: string) => void;
  closeAllTabs: () => void;
}

export const useTabsStore = create<TabsState>((set, get) => ({
  activeKey: '/dashboard',
  tabs: [
    {
      key: '/dashboard',
      label: '首页',
      path: '/dashboard',
      icon: null,
      closable: false, // 仪表盘不能关闭
    },
  ],

  addTab: (tab: TabItem) => {
    const state = get();
    const existingTab = state.tabs.find(t => t.key === tab.key);

    if (!existingTab) {
      set({
        tabs: [...state.tabs, tab],
        activeKey: tab.key,
      });
    } else {
      set({ activeKey: tab.key });
    }
  },

  removeTab: (key: string) => {
    const state = get();
    const newTabs = state.tabs.filter(tab => tab.key !== key);

    // 如果关闭的是当前激活的 tab，需要激活新的 tab
    let newActiveKey = state.activeKey;
    if (state.activeKey === key) {
      if (newTabs.length > 0) {
        // 激活关闭 tab 的前一个 tab，如果没有则激活后一个
        const currentIndex = state.tabs.findIndex(tab => tab.key === key);
        const newIndex = currentIndex > 0 ? currentIndex - 1 : 0;
        newActiveKey = newTabs[newIndex].key;
      } else {
        newActiveKey = '';
      }
    }

    set({
      tabs: newTabs,
      activeKey: newActiveKey,
    });
  },

  setActiveTab: (key: string) => {
    set({ activeKey: key });
  },

  closeOtherTabs: (key: string) => {
    const state = get();
    const dashboardTab = state.tabs.find(tab => tab.key === '/dashboard');
    const currentTab = state.tabs.find(tab => tab.key === key);

    if (dashboardTab && currentTab && dashboardTab.key !== currentTab.key) {
      set({
        tabs: [dashboardTab, currentTab],
        activeKey: key,
      });
    }
  },

  closeLeftTabs: (key: string) => {
    const state = get();
    const index = state.tabs.findIndex(tab => tab.key === key);
    const dashboardTab = state.tabs.find(tab => tab.key === '/dashboard');

    const newTabs = state.tabs.slice(index);
    if (dashboardTab && !newTabs.find(tab => tab.key === '/dashboard')) {
      newTabs.unshift(dashboardTab);
    }

    set({
      tabs: newTabs,
      activeKey: key,
    });
  },

  closeRightTabs: (key: string) => {
    const state = get();
    const index = state.tabs.findIndex(tab => tab.key === key);
    const newTabs = state.tabs.slice(0, index + 1);

    set({
      tabs: newTabs,
      activeKey: key,
    });
  },

  closeAllTabs: () => {
    const dashboardTab = get().tabs.find(tab => tab.key === '/dashboard');
    if (dashboardTab) {
      set({
        tabs: [dashboardTab],
        activeKey: '/dashboard',
      });
    }
  },
}));